#include<vector>
#include<unordered_set>
#include<unordered_map>
using namespace std;
class Twitter {
private:
    const static int limitNumber = 10;
    struct Msg {
        int id;
        int timestamp;
        Msg* prev = NULL;
        Msg* next = NULL;
        Msg(int id, int timestamp) :id(id), timestamp(timestamp), prev(NULL), next(NULL) {

        }
    };
    struct User {
        const static int limitNumber = 10;
        int id;
        Msg* msgs;
        int size;
        unordered_set<int>follows;
        User(int id = 0) :id(id), msgs(NULL), size(0) {}
        void push(int tweetId, int timestamp) {
            if (msgs == NULL) {
                msgs = new Msg(tweetId, timestamp);
                msgs->next = msgs;
                msgs->prev = msgs;
                ++size;
            }
            else {
                if (size < limitNumber) {
                    Msg* msg = new Msg(tweetId, timestamp);
                    msg->next = msgs;
                    msg->prev = msgs->prev;
                    msgs->prev->next = msg;
                    msgs->prev = msg;
                    msgs = msg;
                    ++size;
                }
                else {
                    msgs->prev->id = tweetId;
                    msgs->prev->timestamp = timestamp;
                    msgs = msgs->prev;
                }
            }
        }
    };
    unordered_map<int, User>map;
    int tm = 0;

    User& getUser(int userId) {
        map[userId].id = userId;
        return  map[userId];
    }
public:
    /** Initialize your data structure here. */
    Twitter() {

    }

    /** Compose a new tweet. */
    void postTweet(int userId, int tweetId) {
        getUser(userId);
        map[userId].push(tweetId, tm++);
    }

    /** Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. */
    vector<int> getNewsFeed(int userId) {
        if (map.find(userId) == map.end()) return {};
        vector<int>res;
        vector<Msg*>msgs;
        if (map[userId].msgs != NULL) msgs.push_back(map[userId].msgs);
        for (auto& kv : map[userId].follows) {
            if (map[kv].msgs != NULL) msgs.push_back(map[kv].msgs);
        }
        vector<Msg*>msgs2 = msgs;
        while (res.size() < limitNumber) {
            int i = -1;
            for (int j = 0; j < msgs.size(); ++j) {
                if (msgs[j] == NULL) continue;
                if (i == -1 || msgs[j]->timestamp > msgs[i]->timestamp)
                    i = j;
            }
            if (i == -1) break;
            res.push_back(msgs[i]->id);
            if (msgs[i] == msgs2[i]->prev) msgs[i] = NULL;
            else msgs[i] = msgs[i]->next;
        }
        return res;
    }

    /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
    void follow(int followerId, int followeeId) {
        if (followerId == followeeId) return;
        getUser(followerId);
        getUser(followeeId);
        map[followerId].follows.insert(followeeId);
    }

    /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
    void unfollow(int followerId, int followeeId) {
        if (followerId == followeeId) return;
        if (map.find(followerId) != map.end() && map.find(followeeId) != map.end()) {
            map[followerId].follows.erase(followeeId);
        }
    }
};

int main() {
    Twitter twitter;
    twitter.postTweet(1, 5);
    twitter.getNewsFeed(1);
    twitter.follow(1, 2);
    twitter.postTweet(2, 6);
    twitter.getNewsFeed(1);
}

/**
 * Your Twitter object will be instantiated and called as such:
 * Twitter* obj = new Twitter();
 * obj->postTweet(userId,tweetId);
 * vector<int> param_2 = obj->getNewsFeed(userId);
 * obj->follow(followerId,followeeId);
 * obj->unfollow(followerId,followeeId);
 */